﻿@model ProductModel

@using Telerik.Web.Mvc.UI;

@if (Model.Id > 0)
{
	<div class="alert alert-info alert-dismissible">
		<div>
			@T("Admin.Catalog.Products.BundleItems.NotesOnProductBundles"):
			<a class="ml-2" id="ProductBundleInfoLink" href="javascript:void(0)" data-in="@T("Common.Show")" data-out="@T("Common.Hide")">@T("Common.Show")</a>
		</div>
		<div id="ProductBundleInfoContainer" class="hide">
			@Html.Raw(T("Admin.Catalog.Products.BundleItems.AdminNoteGeneral"))

			@Html.Raw(T(Model.BundlePerItemPricing ? "Admin.Catalog.Products.BundleItems.AdminNotePerItemPricing" : "Admin.Catalog.Products.BundleItems.AdminNoteBundlePricing"))

			@if (Model.BundlePerItemShipping)
			{
				@Html.Raw(T("Admin.Catalog.Products.BundleItems.AdminNotePerItemShipping"))
			}
		</div>
		<button type="button" class="close" data-dismiss="alert">&times;</button>
	</div>

	<div>
        @(Html.LocalizedEditor<ProductModel, ProductLocalizedModel>("product-bundle-localized",
            @<table class="adminContent">
                <tr>
                    <td class="adminTitle">
                        @Html.SmartLabelFor(model => model.Locales[item].BundleTitleText)
                    </td>
                    <td class="adminData">
						@*IMPORTANT: Do not delete, this hidden element contains the id to assign localized values to the corresponding language *@
						@Html.HiddenFor(model => model.Locales[item].LanguageId)

                        @Html.EditorFor(model => model.Locales[item].BundleTitleText)
                        @Html.ValidationMessageFor(model => model.Locales[item].BundleTitleText)
                    </td>
                </tr>
            </table>        
        ,
            @<table class="adminContent">
                <tr>
                    <td class="adminTitle">
                        @Html.SmartLabelFor(model => model.BundleTitleText)
                    </td>
                    <td class="adminData">
                        @Html.EditorFor(model => model.BundleTitleText)
                        @Html.ValidationMessageFor(model => model.BundleTitleText)
                    </td>
                </tr>
            </table>
        ))

        <table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.BundlePerItemPricing)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.BundlePerItemPricing)
                    @Html.ValidationMessageFor(model => model.BundlePerItemPricing)
                </td>
            </tr>
			<tr>
				<td class="adminTitle">
					@Html.SmartLabelFor(model => model.BundlePerItemShipping)
				</td>
				<td class="adminData">
					@Html.EditorFor(model => model.BundlePerItemShipping)
					@Html.ValidationMessageFor(model => model.BundlePerItemShipping)
				</td>
			</tr>
			<tr>
				<td class="adminTitle">
					@Html.SmartLabelFor(model => model.BundlePerItemShoppingCart)
				</td>
				<td class="adminData">
					@Html.EditorFor(model => model.BundlePerItemShoppingCart)
					@Html.ValidationMessageFor(model => model.BundlePerItemShoppingCart)
				</td>
			</tr>
        </table>
    </div>    
    

	<div>
		@(Html.Telerik().Grid<ProductModel.BundleItemModel>()
			.Name("bundleitems-grid")
			.DataKeys(keys => { keys.Add(x => x.Id); })
			.DataBinding(dataBinding =>
			{
				dataBinding.Ajax()
					.Select("BundleItemList", "Product", new { productId = Model.Id })
					.Delete("BundleItemDelete", "Product");
			})
			.Columns(columns =>
			{
				columns.Bound(x => x.ProductName)
					.ReadOnly()
					.Width(520)
					.ClientTemplate(@Html.LabeledProductName("ProductId", "ProductName"));
				columns.Bound(x => x.Sku)
					.ReadOnly();
				columns.Bound(x => x.Quantity)
					.Centered();
				columns.Bound(x => x.Discount)
					.Centered();
				columns.Bound(x => x.Visible)
					.ClientTemplate(@Html.SymbolForBool("Visible"))
					.Centered();
				columns.Bound(x => x.Published)
					.ClientTemplate(@Html.SymbolForBool("Published"))
					.Centered();
				columns.Bound(x => x.DisplayOrder)
					.Centered();
				columns.Command(commands =>
				{
					commands.Custom("edit-bundle-part").Text(T("Common.Edit"));
					commands.Delete().Localize(T);
				}).HtmlAttributes(new { align = "right" });
			})
			.ClientEvents(events => events.OnRowDataBound("onRowDataBound_bundleItems"))
			.ToolBar(commands => commands.Template(GridCommands))
			.EnableCustomBinding(true))
	</div>

    <script>
		function EntPicker_Completed(ids) {
			$({}).doAjax({
				type: 'POST',
				url: '@Url.Action("BundleItemAdd")',
				data: { "productId": @Model.Id, "selectedProductIds": ids },
				callbackSuccess: function () {
					$("#bundleitems-grid").data('tGrid').ajaxRequest();
				}
			});
			return true;
		};

		function onRowDataBound_bundleItems(e) {
			if (!e.dataItem.Published) {
				$(e.row).find('td').wrapInner('<span class="text-muted" />');
			}
		}

		$(function () {
			$('#btnRefreshBundleItems').on('click', function () {
				$("#bundleitems-grid").data('tGrid').ajaxRequest();
				return false;
			});

			// Toggle bundle infos
			$(document).on('click', '#ProductBundleInfoLink', function () {
				var info = $('#ProductBundleInfoContainer');
				$(this).text($(this).attr(info.is(':visible') ? 'data-in' : 'data-out'));
				info.slideToggle(200);
			});

			$('#bundleitems-grid').on('click', '.t-grid-edit-bundle-part', function (e) {
				e.preventDefault();
				var grid = $('#bundleitems-grid').data('tGrid');
				var tr = $(this).closest('tr');
				var id = grid.dataItem(tr).Id;
				var href = '@Url.Content("~/Admin/Product/BundleItemEditPopup/")' + id + '?btnId=btnRefreshBundleItems&formId=product-form';

				openPopup(href);

				return false;
			});
		});
    </script>
}
else
{
    <p class="text-muted">
		@T("Admin.Catalog.Products.BundleItems.SaveBeforeEdit")
	</p>
}

@helper GridCommands(Grid<ProductModel.BundleItemModel> grid)
{
	@(Html.SmartStore().EntityPicker()
		.HtmlAttribute("class", "t-button t-button-primary")
		.DialogTitle(T("Admin.Catalog.Products.BundleItems.AddNew").Text.EncodeJsString('\'', false))
		.Caption(T("Common.AddNew").Text.EncodeJsString('\'', false))
		.DisableBundleProducts(true)
		.DisableGroupedProducts(true)
		.IconCssClass("fa fa-plus")
		.OnSelectionCompleted("EntPicker_Completed"))

	<input type="submit" id="btnRefreshBundleItems" name="btnRefreshBundleItems" class="d-none" />
}